package com.rul.mars.shop.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.rul.mars.common.api.CommonResultCode;
import com.rul.mars.common.constant.AuthConstant;
import com.rul.mars.common.constant.DeleteStatusConstant;
import com.rul.mars.common.constant.ShopStatusConstant;
import com.rul.mars.common.domain.UserDto;
import com.rul.mars.common.exception.Asserts;
import com.rul.mars.mbg.domain.ShopApplicationParam;
import com.rul.mars.mbg.domain.ShopDetail;
import com.rul.mars.mbg.mapper.ShopMapper;
import com.rul.mars.mbg.model.Shop;
import com.rul.mars.mbg.model.ShopExample;
import com.rul.mars.mbg.model.User;
import com.rul.mars.shop.service.ShopService;
import com.rul.mars.shop.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 店铺服务实现
 *
 * @author RuL
 */
@Service
public class ShopServiceImpl implements ShopService {

    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private UserService userService;
    @Autowired
    private HttpServletRequest request;

    /**
     * 店铺申请入驻
     *
     * @param param 店铺申请入驻参数
     * @return 申请结果
     */
    @Override
    public boolean applyForEnter(ShopApplicationParam param) {
        User user = userService.currentUser();

        Shop shop = BeanUtil.copyProperties(param, Shop.class);
        shop.setDeleteStatus(DeleteStatusConstant.NO);
        shop.setStatus(ShopStatusConstant.UNDER_REVIEW);
        shop.setUserId(user.getId());

        int rows = shopMapper.insertSelective(shop);
        return rows == 1;
    }

    /**
     * 店铺申请状态查询
     *
     * @return 店铺详情
     */
    @Override
    public ShopDetail reviewStatus() {
        User user = userService.currentUser();
        //用户ID
        Long userId = user.getId();
        ShopExample example = new ShopExample();
        example.createCriteria().andUserIdEqualTo(userId);
        List<Shop> shops = shopMapper.selectByExample(example);
        if (!CollUtil.isEmpty(shops)) {
            Shop shop = shops.get(0);
            return BeanUtil.copyProperties(shop, ShopDetail.class);
        }
        return null;
    }


    /**
     * 根据店铺ID查询店铺详情
     *
     * @param id 店铺ID
     * @return 店铺详情
     */
    @Override
    public ShopDetail detail(Long id) {
        Shop shop = shopMapper.selectByPrimaryKey(id);
        return BeanUtil.copyProperties(shop, ShopDetail.class);
    }

    /**
     * 查询当前店铺信息
     *
     * @return 当前店铺信息
     */
    @Override
    public Shop current() {
        //从Token中获取用户信息JSON字符串
        String userStr = request.getHeader(AuthConstant.USER_TOKEN_HEADER);
        if (StrUtil.isEmpty(userStr)) {
            Asserts.fail(CommonResultCode.UNAUTHORIZED);
        }
        //将JSON字符串转为UserDto对象
        UserDto userDto = JSONUtil.toBean(userStr, UserDto.class);

        Long userId = userDto.getId();
        ShopExample example = new ShopExample();
        example.createCriteria()
                .andUserIdEqualTo(userId);

        List<Shop> shops = shopMapper.selectByExample(example);

        if (!CollUtil.isEmpty(shops)) {
            return shops.get(0);
        }
        return null;
    }

}
